﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MvcEngine.Blog.Repositories;
using MvcEngine.Blog.Models.Entities;
using MvcEngine.LinqToSqlProvider;

namespace MvcEngine.Blog.LinqToSqlProvider
{
    public class BlogPostNotifyRepository : Repository, IBlogPostNotifyRepository
    {
        public IList<BlogPostNotify> GetAll(Guid postId)
        {
            using (var contextManager = CreateContextManager(true))
            {
                return (from c in contextManager.Context.Notifies()
                        where c.PostId == postId
                        orderby c.CreatedDate
                        select c).ToList();
            }
        }

        public bool Exists(Guid postId, string email)
        {
            using (var contextManager = CreateContextManager(true))
            {
                return contextManager.Context.Notifies().Any(n => n.PostId == postId && n.NotifyAddress == email);
            }
        }

        public void Add(BlogPostNotify entity)
        {
            using (var contextManager = CreateContextManager(false))
            {
                contextManager.Context.Notifies().InsertOnSubmit(entity);
                contextManager.Commit();
            }
        }

        public void DeleteAll(Guid postId)
        {
            var notifies = GetAll(postId);
            using (var contextManager = CreateContextManager(false))
            {
                contextManager.Context.Notifies().DeleteAllOnSubmit(notifies);
                contextManager.Commit();
            }
        }
    }
}
